package coder

import (
	"github.com/lishimeng/app-starter/tool"
	"github.com/lishimeng/go-log"
	"time"
)

func (c *clientImpl) WaitResp(h func([]byte) error) (err error) {
	log.Debug("wait resp.")
	select {
	case <-time.After(time.Second * 20):
		log.Debug("timeout")
		err = ErrTimeout
	case p := <-c.cmdCtx.p:
		e := h([]byte(p))
		if e != nil {
			err = e
		}
	}
	return
}

func (c *clientImpl) tx(p []byte) (int, error) {
	log.Debug(">>%s", tool.BytesToHex(p))
	n, err := c.proxy.Write(p)

	if err != nil {
		panic(err) // TODO
	}
	return n, err
}
